# Clean up
rm(list = ls())
Sys.setenv(LANG = "en")

# Set working directory: please set your own
#setwd("~/Dropbox/sanctions_replication/")

# Load necessary packages
library(tidyverse)
library(readstata13)

# Import dataset
reported_vote_data <- read.dta13("data/reported_vote_all.dta")

# Calculate the mean values by 'official' and 'Party'
mean_vote_data <- reported_vote_data %>%
  rename(vote_official = official_vote) %>%
  pivot_longer(cols = c(vote_official:vote_cis),
               names_to = "official",
               values_to = "vote") %>%
  group_by(official, Party) %>%
  summarize(mean_vote = mean(vote, na.rm = TRUE))

mean_vote_data$official[mean_vote_data$official == "vote_cis"] <- "Vote as reported in post-electoral surveys"
mean_vote_data$official[mean_vote_data$official == "vote_official"] <- "Official vote for the party"

# Make the plot
figureb1 <- ggplot(mean_vote_data, aes(x = official, y = mean_vote, fill = official)) +
  geom_bar(stat = "identity", alpha = 0.3, color = "black") +
  scale_fill_manual(values = c("gray40", "blue")) +
  theme_bw() +
  facet_wrap(~ Party, dir = "v", scales = "free_y") +
  labs(y = "Vote percentage", x = " ") +
  theme(legend.position = "none")
figureb1

ggsave("figures/figureb1.png", plot = figureb1, width = 15, height = 15, units = "cm")